import yaml

class Yaml:
    def __init__(self, file_path):
        self.file_path = file_path

    def read_yaml(self):
        with open(self.file_path, 'r',encoding='utf-8') as file:
            data = yaml.safe_load(file)
        return data

    def write_yaml(self, yaml_data,encoding='utf-8'):
        # 读取现有数据
        try:
            with open(self.file_path, 'r') as file:
                existing_data = yaml.safe_load(file)
        except FileNotFoundError:
            # 如果文件不存在，则创建一个空的字典
            existing_data = {}

        # 如果 existing_data 是 None，说明文件是空的，创建一个空的字典
        if existing_data is None:
            existing_data = {}

        # 合并现有数据和新的数据
        existing_data.update(yaml_data)

        # 写回数据到文件
        with open(self.file_path, 'w') as file:
            yaml.dump(existing_data, file)
        return existing_data

















